package top.lishuoboy.knife4j.swagger3_0_0.controller;

import cn.hutool.core.lang.RegexPool;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import top.lishuoboy.knife4j.swagger3_0_0.annotation.NotResponseBodyAdvice;
import top.lishuoboy.knife4j.swagger3_0_0.entry.User;
import top.lishuoboy.knife4j.swagger3_0_0.enums.Code;
import top.lishuoboy.knife4j.swagger3_0_0.vo.RespVo;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;

@Api(tags = "用户管理")
@RestController
@RequestMapping("user")
public class MyUserController {

    /** 获取用户 */
    @ApiOperation(value = "获取用户", notes = "获取用户详细说明")
    @ApiImplicitParam(name = "implicitParam1", value = "隐式参数1", required = true)
//    @ApiImplicitParams({
//        @ApiImplicitParam(name = "implicitParam2", value = "隐式参数2"),
//        @ApiImplicitParam(name = "implicitParam3", value = "隐式参数3")
//    })
    // 响应码说明
    @ApiResponses({
        @ApiResponse(code = 404, message = "Not Found页面没找到"),
        @ApiResponse(code = 888, message = "你猜"),
    })
    @GetMapping("getUserById")
    public User getUserById(@ApiParam(name = "id", value = "用户ID", required = true) int id) {
        return User.builder().id(1).userName("张三").build();
    }

    /** 加了注解@NotResponseBodyAdvice，不会被RespVo封装 */
    @ApiOperation(value = "获取用户2", notes = "加了注解@NotResponseBodyAdvice，不会被RespVo封装")
    @NotResponseBodyAdvice
    @GetMapping("getUserById2")
    public User getUserById2(@ApiParam(name = "id", value = "用户ID", required = true) int id) {
        return User.builder().id(1).userName("张三").build();
    }

    /** 返回类型已是RespVo，不会被RespVo封装 */
    @ApiOperation(value = "获取用户3", notes = "返回类型已是RespVo，不会被RespVo封装")
    @GetMapping("getUserById3")
    public RespVo<User> getUserById3(@ApiParam(name = "id", value = "用户ID", required = true) int id) {
        return RespVo.<User>builder().code(Code.SUCCESS.code).msg(Code.SUCCESS.msg)
            .data(User.builder().id(1).userName("张三").build()).build();
    }

    @ApiOperation(value = "删除用户byId")
    @GetMapping("delUserById")
    public Object delUserById(@ApiParam(value = "用户ID") @Pattern(regexp = RegexPool.NUMBERS) Integer flag) {
        if (flag == 1) {
            System.out.println(1 / 0);  // 造异常
            return "SUCCESS";
        } else {
            return "SUCCESS";
        }
    }


    /** 新增用户 */
    @ApiOperation(value = "新增用户", notes = "新增用户详细说明")
    @PostMapping("/postUser")
    public void postUser(@RequestBody @Valid User user) {
        System.out.println(user);
//        return StrUtil.format("【postUser】。user=={}", user);
    }

    @RequestMapping("/requestMethod")
    public String requestMethod() {
        return StrUtil.format("【requestMethod】");
    }

    @ApiIgnore  // 忽略，当前注解描述的方法或类型，不生成api帮助文档。
    @RequestMapping("/apiIgnore")
    public String apiIgnore() {
        return StrUtil.format("【apiIgnore】");
    }
}